공간분석과 공간통계: GIS 버퍼분석 & 중첩분석 - Part 1 (basics of geopandas & folium)¶

가천대학교 공간정보시스템(14842001) 장요한 (ycanns@gachon.ac.kr)¶


1. Initialization¶

In [52]:
import sys
!{sys.executable} -m pip install geopandas
!{sys.executable} -m pip install cartoframes
# ! pip install geopandas
# ! pip install cartoframes
Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Requirement already satisfied: geopandas in /usr/local/lib/python3.10/dist-packages (0.12.2)
Requirement already satisfied: pyproj>=2.6.1.post1 in /usr/local/lib/python3.10/dist-packages (from geopandas) (3.5.0)
Requirement already satisfied: shapely>=1.7 in /usr/local/lib/python3.10/dist-packages (from geopandas) (2.0.1)
Requirement already satisfied: fiona>=1.8 in /usr/local/lib/python3.10/dist-packages (from geopandas) (1.9.3)
Requirement already satisfied: pandas>=1.0.0 in /usr/local/lib/python3.10/dist-packages (from geopandas) (1.5.3)
Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from geopandas) (23.1)
Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from fiona>=1.8->geopandas) (2022.12.7)
Requirement already satisfied: attrs>=19.2.0 in /usr/local/lib/python3.10/dist-packages (from fiona>=1.8->geopandas) (23.1.0)
Requirement already satisfied: click~=8.0 in /usr/local/lib/python3.10/dist-packages (from fiona>=1.8->geopandas) (8.1.3)
Requirement already satisfied: click-plugins>=1.0 in /usr/local/lib/python3.10/dist-packages (from fiona>=1.8->geopandas) (1.1.1)
Requirement already satisfied: cligj>=0.5 in /usr/local/lib/python3.10/dist-packages (from fiona>=1.8->geopandas) (0.7.2)
Requirement already satisfied: munch>=2.3.2 in /usr/local/lib/python3.10/dist-packages (from fiona>=1.8->geopandas) (2.5.0)
Requirement already satisfied: python-dateutil>=2.8.1 in /usr/local/lib/python3.10/dist-packages (from pandas>=1.0.0->geopandas) (2.8.2)
Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas>=1.0.0->geopandas) (2022.7.1)
Requirement already satisfied: numpy>=1.21.0 in /usr/local/lib/python3.10/dist-packages (from pandas>=1.0.0->geopandas) (1.22.4)
Requirement already satisfied: six in /usr/local/lib/python3.10/dist-packages (from munch>=2.3.2->fiona>=1.8->geopandas) (1.16.0)
Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Requirement already satisfied: cartoframes in /usr/local/lib/python3.10/dist-packages (1.2.5)
Requirement already satisfied: markupsafe<=2.0.1 in /usr/local/lib/python3.10/dist-packages (from cartoframes) (2.0.1)
Requirement already satisfied: geopandas<1.0,>=0.6.0 in /usr/local/lib/python3.10/dist-packages (from cartoframes) (0.12.2)
Requirement already satisfied: pandas>=0.25.0 in /usr/local/lib/python3.10/dist-packages (from cartoframes) (1.5.3)
Requirement already satisfied: appdirs<2.0,>=1.4.3 in /usr/local/lib/python3.10/dist-packages (from cartoframes) (1.4.4)
Requirement already satisfied: semantic-version<3,>=2.8.0 in /usr/local/lib/python3.10/dist-packages (from cartoframes) (2.10.0)
Requirement already satisfied: unidecode<2.0,>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from cartoframes) (1.3.6)
Requirement already satisfied: jinja2<3.0,>=2.10.1 in /usr/local/lib/python3.10/dist-packages (from cartoframes) (2.11.3)
Requirement already satisfied: carto<2.0,>=1.11.3 in /usr/local/lib/python3.10/dist-packages (from cartoframes) (1.11.3)
Requirement already satisfied: pyrestcli==0.6.11 in /usr/local/lib/python3.10/dist-packages (from carto<2.0,>=1.11.3->cartoframes) (0.6.11)
Requirement already satisfied: requests>=2.7.0 in /usr/local/lib/python3.10/dist-packages (from carto<2.0,>=1.11.3->cartoframes) (2.27.1)
Requirement already satisfied: python-dateutil>=2.5.3 in /usr/local/lib/python3.10/dist-packages (from pyrestcli==0.6.11->carto<2.0,>=1.11.3->cartoframes) (2.8.2)
Requirement already satisfied: future>=0.15.2 in /usr/local/lib/python3.10/dist-packages (from pyrestcli==0.6.11->carto<2.0,>=1.11.3->cartoframes) (0.18.3)
Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from geopandas<1.0,>=0.6.0->cartoframes) (23.1)
Requirement already satisfied: pyproj>=2.6.1.post1 in /usr/local/lib/python3.10/dist-packages (from geopandas<1.0,>=0.6.0->cartoframes) (3.5.0)
Requirement already satisfied: fiona>=1.8 in /usr/local/lib/python3.10/dist-packages (from geopandas<1.0,>=0.6.0->cartoframes) (1.9.3)
Requirement already satisfied: shapely>=1.7 in /usr/local/lib/python3.10/dist-packages (from geopandas<1.0,>=0.6.0->cartoframes) (2.0.1)
Requirement already satisfied: numpy>=1.21.0 in /usr/local/lib/python3.10/dist-packages (from pandas>=0.25.0->cartoframes) (1.22.4)
Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas>=0.25.0->cartoframes) (2022.7.1)
Requirement already satisfied: attrs>=19.2.0 in /usr/local/lib/python3.10/dist-packages (from fiona>=1.8->geopandas<1.0,>=0.6.0->cartoframes) (23.1.0)
Requirement already satisfied: click-plugins>=1.0 in /usr/local/lib/python3.10/dist-packages (from fiona>=1.8->geopandas<1.0,>=0.6.0->cartoframes) (1.1.1)
Requirement already satisfied: cligj>=0.5 in /usr/local/lib/python3.10/dist-packages (from fiona>=1.8->geopandas<1.0,>=0.6.0->cartoframes) (0.7.2)
Requirement already satisfied: munch>=2.3.2 in /usr/local/lib/python3.10/dist-packages (from fiona>=1.8->geopandas<1.0,>=0.6.0->cartoframes) (2.5.0)
Requirement already satisfied: click~=8.0 in /usr/local/lib/python3.10/dist-packages (from fiona>=1.8->geopandas<1.0,>=0.6.0->cartoframes) (8.1.3)
Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from fiona>=1.8->geopandas<1.0,>=0.6.0->cartoframes) (2022.12.7)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.5.3->pyrestcli==0.6.11->carto<2.0,>=1.11.3->cartoframes) (1.16.0)
Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.10/dist-packages (from requests>=2.7.0->carto<2.0,>=1.11.3->cartoframes) (2.0.12)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.7.0->carto<2.0,>=1.11.3->cartoframes) (3.4)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.7.0->carto<2.0,>=1.11.3->cartoframes) (1.26.15)
In [53]:
import pandas as pd
import geopandas as gpd
from cartoframes.viz import Layer, color_category_style
from shapely.geometry import Polygon, Point, LineString
from shapely.ops import transform
from functools import partial
import pyproj
import numpy as np, matplotlib.pyplot as plt
plt.style.use('ggplot')
import folium
from folium.plugins import FloatImage





import os

2. Import DATA¶

In [ ]:
# 필요한 파일 업로드 기능 (구글 코랩)
from google.colab import files
uploaded = files.upload()
data_folder = os. getcwd()
Upload widget is only available when the cell has been executed in the current browser session. Please rerun this cell to enable.
Saving LARD_ADM_SECT_SGG_11.dbf to LARD_ADM_SECT_SGG_11.dbf
Saving LARD_ADM_SECT_SGG_11.prj to LARD_ADM_SECT_SGG_11.prj
Saving LARD_ADM_SECT_SGG_11.shp to LARD_ADM_SECT_SGG_11.shp
Saving LARD_ADM_SECT_SGG_11.shx to LARD_ADM_SECT_SGG_11.shx
In [54]:
# 구글 드리이브에 파일이 있는 경우
data_folder = "/content/drive/MyDrive/sample_files/"
In [55]:
data_folder
Out[55]:
'/content/drive/MyDrive/sample_files/'
In [56]:
# 시군구(SGG) 와 읍면동(UMD) GIS 파일 읽어오기
Seoul_SGG = gpd.read_file(data_folder  + "LARD_ADM_SECT_SGG_서울/LARD_ADM_SECT_SGG_11.shp", encoding='cp949')
Seoul_UMD = gpd.read_file(data_folder  + "LSMD_ADM_SECT_UMD_서울/LSMD_ADM_SECT_UMD_11.shp", encoding='cp949')

Kyggi_SGG = gpd.read_file(data_folder  + "LARD_ADM_SECT_SGG_경기/LARD_ADM_SECT_SGG_41.shp", encoding='cp949')
Kyggi_UMD = gpd.read_file(data_folder  + "LSMD_ADM_SECT_UMD_경기/LSMD_ADM_SECT_UMD_41.shp", encoding='cp949')
In [57]:
print(Seoul_SGG)
   ADM_SECT_C SGG_NM  SGG_OID COL_ADM_SE  GID  \
0       11740    강동구      NaN      11740  125   
1       11710    송파구      NaN      11710  126   
2       11680    강남구      NaN      11680  127   
3       11650    서초구      NaN      11650  128   
4       11620    관악구      NaN      11620  129   
5       11590    동작구      NaN      11590  130   
6       11560   영등포구      NaN      11560  131   
7       11545    금천구      NaN      11545  132   
8       11530    구로구      NaN      11530  133   
9       11500    강서구      NaN      11500  134   
10      11470    양천구      NaN      11470  135   
11      11440    마포구      NaN      11440  136   
12      11410   서대문구      NaN      11410  137   
13      11380    은평구      NaN      11380  138   
14      11350    노원구      NaN      11350  139   
15      11320    도봉구      NaN      11320  140   
16      11305    강북구      NaN      11305  141   
17      11290    성북구      NaN      11290  142   
18      11260    중랑구      NaN      11260  143   
19      11230   동대문구      NaN      11230  144   
20      11215    광진구      NaN      11215  145   
21      11200    성동구      NaN      11200  146   
22      11170    용산구      NaN      11170  147   
23      11140     중구      NaN      11140  148   
24      11110    종로구      NaN      11110  149   

                                             geometry  
0   POLYGON ((971595.075 1952405.815, 971596.036 1...  
1   POLYGON ((965821.957 1949386.153, 965816.737 1...  
2   POLYGON ((959331.597 1948602.068, 959342.021 1...  
3   POLYGON ((956982.039 1947144.037, 956982.518 1...  
4   POLYGON ((949438.997 1944127.713, 949456.647 1...  
5   POLYGON ((951811.770 1946444.671, 951885.519 1...  
6   POLYGON ((945280.001 1950949.319, 945361.246 1...  
7   POLYGON ((944705.924 1943086.537, 944706.472 1...  
8   POLYGON ((945165.294 1946679.190, 945362.702 1...  
9   POLYGON ((939006.015 1955916.461, 939036.124 1...  
10  POLYGON ((944706.568 1949917.649, 944717.625 1...  
11  POLYGON ((946043.982 1954091.886, 946130.912 1...  
12  POLYGON ((951688.107 1956065.481, 951687.602 1...  
13  POLYGON ((951637.438 1961851.784, 951649.915 1...  
14  POLYGON ((965666.474 1959953.152, 965671.165 1...  
15  POLYGON ((957223.835 1961199.545, 957195.758 1...  
16  POLYGON ((956316.789 1965175.345, 956401.551 1...  
17  POLYGON ((956787.318 1953542.506, 956761.258 1...  
18  POLYGON ((965698.531 1957987.355, 965772.290 1...  
19  POLYGON ((962141.919 1956519.461, 962149.853 1...  
20  POLYGON ((964825.058 1952633.250, 964875.565 1...  
21  POLYGON ((959681.109 1952649.605, 959842.412 1...  
22  POLYGON ((953115.761 1950834.084, 953114.206 1...  
23  POLYGON ((957890.386 1952616.746, 957909.908 1...  
24  POLYGON ((956615.453 1953567.199, 956621.579 1...  
In [58]:
print(Seoul_UMD)
       EMD_CD EMD_NM  SGG_OID COL_ADM_SE   GID  \
0    11110154    장사동     4315      11110  3037   
1    11110153    돈의동     4314      11110  3038   
2    11290134    길음동     4532      11290  2820   
3    11290133    정릉동     4531      11290  2821   
4    11290132  보문동3가     4530      11290  2822   
..        ...    ...      ...        ...   ...   
462  11680110   압구정동     1049      11680  2652   
463  11680108    논현동     1183      11680  2653   
464  11680107    신사동     1050      11680  2654   
465  11680106    대치동     1215      11680  2655   
466  11680105    삼성동     1184      11680  2656   

                                              geometry  
0    POLYGON ((955438.037 1952426.894, 955438.325 1...  
1    POLYGON ((955054.604 1952786.670, 955055.964 1...  
2    POLYGON ((957364.694 1957374.485, 957376.240 1...  
3    POLYGON ((957598.059 1956308.659, 957583.162 1...  
4    POLYGON ((957323.502 1954285.821, 957323.592 1...  
..                                                 ...  
462  POLYGON ((957027.864 1947810.500, 956989.636 1...  
463  POLYGON ((959686.454 1945763.984, 959679.043 1...  
464  MULTIPOLYGON (((957392.555 1947055.361, 957582...  
465  POLYGON ((960137.158 1945122.806, 960275.714 1...  
466  POLYGON ((959462.166 1946535.406, 959468.699 1...  

[467 rows x 6 columns]
In [59]:
print(Kyggi_SGG)
   ADM_SECT_C   SGG_NM  SGG_OID COL_ADM_SE  GID  \
0       41670      여주시      1.0      41670  255   
1       41830      양평군      NaN      41830  202   
2       41820      가평군      NaN      41820  203   
3       41800      연천군      NaN      41800  204   
4       41670      여주시      NaN      41670  205   
5       41650      포천시      NaN      41650  206   
6       41630      양주시      NaN      41630  207   
7       41610      광주시      NaN      41610  208   
8       41590      화성시      NaN      41590  209   
9       41570      김포시      NaN      41570  210   
10      41550      안성시      NaN      41550  211   
11      41500      이천시      NaN      41500  212   
12      41480      파주시      NaN      41480  213   
13      41465   용인시수지구      NaN      41460  214   
14      41463   용인시기흥구      NaN      41460  215   
15      41461   용인시처인구      NaN      41460  216   
16      41450      하남시      NaN      41450  217   
17      41430      의왕시      NaN      41430  218   
18      41410      군포시      NaN      41410  219   
19      41390      시흥시      NaN      41390  220   
20      41370      오산시      NaN      41370  221   
21      41360     남양주시      NaN      41360  222   
22      41310      구리시      NaN      41310  223   
23      41290      과천시      NaN      41290  224   
24      41287  고양시일산서구      NaN      41280  225   
25      41285  고양시일산동구      NaN      41280  226   
26      41281   고양시덕양구      NaN      41280  227   
27      41273   안산시단원구      NaN      41270  228   
28      41271   안산시상록구      NaN      41270  229   
29      41250     동두천시      NaN      41250  230   
30      41220      평택시      NaN      41220  231   
31      41210      광명시      NaN      41210  232   
32      41190      부천시      NaN      41190  233   
33      41173   안양시동안구      NaN      41170  234   
34      41171   안양시만안구      NaN      41170  235   
35      41150     의정부시      NaN      41150  236   
36      41135  성남시 분당구      NaN      41130  237   
37      41133  성남시 중원구      NaN      41130  238   
38      41131  성남시 수정구      NaN      41130  239   
39      41117  수원시 영통구      NaN      41110  240   
40      41115  수원시 팔달구   3635.0      41110  241   
41      41113  수원시 권선구      NaN      41110  242   
42      41111  수원시 장안구   3656.0      41110  243   
43      41171   안양시만안구   1068.0      41170  253   

                                             geometry  
0   POLYGON ((1197798.732 2017143.238, 1197805.333...  
1   POLYGON ((982850.492 1946970.632, 982851.709 1...  
2   POLYGON ((995776.586 2003840.576, 995850.072 2...  
3   MULTIPOLYGON (((969139.245 2026871.151, 969278...  
4   POLYGON ((1001702.361 1937472.187, 1001797.089...  
5   POLYGON ((966892.447 1974877.390, 966891.483 1...  
6   POLYGON ((947368.314 1980734.088, 947376.200 1...  
7   POLYGON ((967460.104 1927897.516, 967474.627 1...  
8   MULTIPOLYGON (((937367.276 1908984.009, 937378...  
9   MULTIPOLYGON (((918342.597 1954626.063, 918318...  
10  POLYGON ((979061.612 1906358.774, 979230.283 1...  
11  POLYGON ((997562.338 1928986.116, 997563.006 1...  
12  POLYGON ((926029.845 1982607.318, 926030.368 1...  
13  POLYGON ((958355.298 1930444.111, 958363.464 1...  
14  POLYGON ((961454.846 1919008.711, 961454.703 1...  
15  POLYGON ((965242.608 1902868.747, 965243.843 1...  
16  POLYGON ((973085.483 1954673.512, 973158.061 1...  
17  POLYGON ((959478.476 1935254.408, 959508.382 1...  
18  POLYGON ((944430.306 1925467.989, 944440.936 1...  
19  MULTIPOLYGON (((930692.442 1923514.863, 930684...  
20  POLYGON ((958045.182 1911397.377, 958056.659 1...  
21  POLYGON ((979370.959 1975675.372, 979373.679 1...  
22  POLYGON ((968835.313 1960459.584, 968952.537 1...  
23  POLYGON ((956160.900 1940996.512, 956172.751 1...  
24  POLYGON ((935391.920 1967763.154, 935406.115 1...  
25  POLYGON ((936300.697 1964630.223, 936357.822 1...  
26  POLYGON ((949705.274 1972247.889, 949753.252 1...  
27  MULTIPOLYGON (((907924.514 1898684.053, 907934...  
28  POLYGON ((945251.730 1931095.635, 945295.612 1...  
29  POLYGON ((956597.493 1990708.563, 956623.669 1...  
30  POLYGON ((940272.495 1883450.667, 940725.204 1...  
31  POLYGON ((944265.786 1944083.761, 944283.892 1...  
32  POLYGON ((940109.914 1945420.200, 940119.450 1...  
33  POLYGON ((952571.411 1938090.489, 952708.664 1...  
34  POLYGON ((949973.055 1938381.382, 949983.015 1...  
35  POLYGON ((965342.976 1975684.509, 965374.915 1...  
36  POLYGON ((962459.288 1933176.518, 962459.447 1...  
37  POLYGON ((971787.037 1941627.348, 971794.930 1...  
38  POLYGON ((968165.515 1941710.496, 968177.333 1...  
39  POLYGON ((962303.378 1918638.005, 962306.340 1...  
40  POLYGON ((953671.615 1921174.740, 953688.481 1...  
41  POLYGON ((958952.060 1915432.098, 958913.531 1...  
42  POLYGON ((957637.792 1928018.659, 957660.284 1...  
43  POLYGON ((944864.448 2034792.615, 944906.020 2...  
In [60]:
print(Kyggi_UMD)
       EMD_CD EMD_NM  SGG_OID COL_ADM_SE   GID  \
0    41190123    삼정동     3954      41190  5180   
1    41190124     내동     3955      41190  5181   
2    41190115    옥길동     3956      41190  5182   
3    41570107    풍무동     3626      41570  5137   
4    41630250    백석읍     3156      41630  5138   
..        ...    ...      ...        ...   ...   
768  41570109    구래동     2868      41570  4332   
769  41570108    마산동     2867      41570  4333   
770  41570106    사우동     2865      41570  4335   
771  41570105    감정동     2864      41570  4336   
772  41570104    장기동     2863      41570  4337   

                                              geometry  
0    POLYGON ((935886.449 1946412.474, 935883.109 1...  
1    POLYGON ((935886.449 1946412.474, 935879.700 1...  
2    POLYGON ((938741.219 1940538.468, 938746.599 1...  
3    POLYGON ((929738.402 1956431.399, 929774.674 1...  
4    POLYGON ((949620.351 1973195.655, 949766.405 1...  
..                                                 ...  
768  POLYGON ((923368.224 1960146.021, 923164.387 1...  
769  POLYGON ((923040.651 1959879.175, 923032.414 1...  
770  POLYGON ((932246.601 1959081.705, 932274.420 1...  
771  POLYGON ((927387.990 1959971.222, 927385.986 1...  
772  POLYGON ((925170.717 1960745.910, 925186.618 1...  

[773 rows x 6 columns]
In [61]:
Seoul_SGG.head()
Out[61]:
ADM_SECT_C SGG_NM SGG_OID COL_ADM_SE GID geometry
0 11740 강동구 NaN 11740 125 POLYGON ((971595.075 1952405.815, 971596.036 1...
1 11710 송파구 NaN 11710 126 POLYGON ((965821.957 1949386.153, 965816.737 1...
2 11680 강남구 NaN 11680 127 POLYGON ((959331.597 1948602.068, 959342.021 1...
3 11650 서초구 NaN 11650 128 POLYGON ((956982.039 1947144.037, 956982.518 1...
4 11620 관악구 NaN 11620 129 POLYGON ((949438.997 1944127.713, 949456.647 1...
In [62]:
Seoul_UMD.head()
Out[62]:
EMD_CD EMD_NM SGG_OID COL_ADM_SE GID geometry
0 11110154 장사동 4315 11110 3037 POLYGON ((955438.037 1952426.894, 955438.325 1...
1 11110153 돈의동 4314 11110 3038 POLYGON ((955054.604 1952786.670, 955055.964 1...
2 11290134 길음동 4532 11290 2820 POLYGON ((957364.694 1957374.485, 957376.240 1...
3 11290133 정릉동 4531 11290 2821 POLYGON ((957598.059 1956308.659, 957583.162 1...
4 11290132 보문동3가 4530 11290 2822 POLYGON ((957323.502 1954285.821, 957323.592 1...
In [63]:
Kyggi_SGG.head()
Out[63]:
ADM_SECT_C SGG_NM SGG_OID COL_ADM_SE GID geometry
0 41670 여주시 1.0 41670 255 POLYGON ((1197798.732 2017143.238, 1197805.333...
1 41830 양평군 NaN 41830 202 POLYGON ((982850.492 1946970.632, 982851.709 1...
2 41820 가평군 NaN 41820 203 POLYGON ((995776.586 2003840.576, 995850.072 2...
3 41800 연천군 NaN 41800 204 MULTIPOLYGON (((969139.245 2026871.151, 969278...
4 41670 여주시 NaN 41670 205 POLYGON ((1001702.361 1937472.187, 1001797.089...
In [ ]:
Kyggi_UMD.head()
Out[ ]:
EMD_CD EMD_NM SGG_OID COL_ADM_SE GID geometry
0 41190123 삼정동 3954 41190 5180 POLYGON ((935886.449 1946412.474, 935883.109 1...
1 41190124 내동 3955 41190 5181 POLYGON ((935886.449 1946412.474, 935879.700 1...
2 41190115 옥길동 3956 41190 5182 POLYGON ((938741.219 1940538.468, 938746.599 1...
3 41570107 풍무동 3626 41570 5137 POLYGON ((929738.402 1956431.399, 929774.674 1...
4 41630250 백석읍 3156 41630 5138 POLYGON ((949620.351 1973195.655, 949766.405 1...

3. Visualizing DATA¶

In [64]:
# GIS 데이터 Quick Checking: 서울 시군구 파일
plt.rcParams['figure.figsize']=(10,20)
Seoul_SGG.plot()
plt.show()
In [65]:
# GIS 데이터 Quick Checking: 서울 읍면동 파일
plt.rcParams['figure.figsize']=(10,20)
Seoul_UMD.plot()
plt.show()
In [66]:
# GIS 데이터 Quick Checking: 경기 시군구 파일
plt.rcParams['figure.figsize']=(10,20)
Kyggi_SGG.plot()
plt.show()
In [67]:
# GIS 데이터 Quick Checking: 경기 읍면동 파일
plt.rcParams['figure.figsize']=(10,20)
Kyggi_UMD.plot()
plt.show()
In [68]:
Kyggi_SGG['SGG_NM'].unique()
Out[68]:
array(['여주시', '양평군', '가평군', '연천군', '포천시', '양주시', '광주시', '화성시', '김포시',
       '안성시', '이천시', '파주시', '용인시수지구', '용인시기흥구', '용인시처인구', '하남시', '의왕시',
       '군포시', '시흥시', '오산시', '남양주시', '구리시', '과천시', '고양시일산서구', '고양시일산동구',
       '고양시덕양구', '안산시단원구', '안산시상록구', '동두천시', '평택시', '광명시', '부천시',
       '안양시동안구', '안양시만안구', '의정부시', '성남시 분당구', '성남시 중원구', '성남시 수정구',
       '수원시 영통구', '수원시 팔달구', '수원시 권선구', '수원시 장안구'], dtype=object)
In [69]:
Kyggi_SGG[Kyggi_SGG['SGG_NM']=='성남시 분당구']
Out[69]:
ADM_SECT_C SGG_NM SGG_OID COL_ADM_SE GID geometry
36 41135 성남시 분당구 NaN 41130 237 POLYGON ((962459.288 1933176.518, 962459.447 1...
In [70]:
Kyggi_SGG[(Kyggi_SGG['SGG_NM']=='성남시 분당구') | (Kyggi_SGG['SGG_NM']=='성남시 중원구') | (Kyggi_SGG['SGG_NM']=='성남시 수정구')]
Out[70]:
ADM_SECT_C SGG_NM SGG_OID COL_ADM_SE GID geometry
36 41135 성남시 분당구 NaN 41130 237 POLYGON ((962459.288 1933176.518, 962459.447 1...
37 41133 성남시 중원구 NaN 41130 238 POLYGON ((971787.037 1941627.348, 971794.930 1...
38 41131 성남시 수정구 NaN 41130 239 POLYGON ((968165.515 1941710.496, 968177.333 1...
In [71]:
# GIS 데이터 Quick Checking: 경기 읍면동 파일
plt.rcParams['figure.figsize']=(10,20)
Kyggi_SGG[Kyggi_SGG['SGG_NM']=='성남시 분당구'].plot()
plt.show()
In [72]:
# GIS 데이터 Quick Checking: 경기 읍면동 파일
plt.rcParams['figure.figsize']=(10,20)
Kyggi_SGG[(Kyggi_SGG['SGG_NM']=='성남시 분당구') | (Kyggi_SGG['SGG_NM']=='성남시 중원구') | (Kyggi_SGG['SGG_NM']=='성남시 수정구')].plot()
plt.show()
In [73]:
map = folium.Map(location=[37.45531, 127.13410], zoom_start=10, tiles='OpenStreet Map', control_scale = True)
folium.GeoJson(data=Seoul_SGG["geometry"], name='서울 시군구').add_to(map)
#folium.GeoJson(data=Seoul_UMD["geometry"], name='서울 읍면동').add_to(map)
folium.GeoJson(data=Kyggi_SGG["geometry"], name='경기 시군구').add_to(map)
#folium.GeoJson(data=Kyggi_UMD["geometry"], name='경기 읍면동').add_to(map)

# add a legend diagram to map
compass_rose = folium.FeatureGroup('compass rose')
FloatImage('https://upload.wikimedia.org/wikipedia/commons/9/99/Compass_rose_simple.svg', bottom =80, left = 7).add_to(compass_rose)
compass_rose.add_to(map)

# later control
folium.LayerControl(collapsed=False).add_to(map)

map
Out[73]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [74]:
# 관심 위/경도 좌표 설정
# 위도
latitude = 37.45531
# 경도
longitude = 127.13410


folium.Marker([latitude, longitude],
              popup=[latitude, longitude],
              tooltip="가천대학교 AI관").add_to(map)
map
Out[74]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [75]:
# 단순 버퍼 표시하기 (실측 거리 X)
folium.CircleMarker([latitude, longitude],
                    color='tomato',
                    radius = 100, 
              tooltip="가천대학교 AI관").add_to(map)
map
Out[75]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [76]:
!pip install mapclassify
Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Requirement already satisfied: mapclassify in /usr/local/lib/python3.10/dist-packages (2.5.0)
Requirement already satisfied: scikit-learn in /usr/local/lib/python3.10/dist-packages (from mapclassify) (1.2.2)
Requirement already satisfied: numpy>=1.3 in /usr/local/lib/python3.10/dist-packages (from mapclassify) (1.22.4)
Requirement already satisfied: pandas>=1.0 in /usr/local/lib/python3.10/dist-packages (from mapclassify) (1.5.3)
Requirement already satisfied: scipy>=1.0 in /usr/local/lib/python3.10/dist-packages (from mapclassify) (1.10.1)
Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from mapclassify) (3.1)
Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas>=1.0->mapclassify) (2022.7.1)
Requirement already satisfied: python-dateutil>=2.8.1 in /usr/local/lib/python3.10/dist-packages (from pandas>=1.0->mapclassify) (2.8.2)
Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn->mapclassify) (3.1.0)
Requirement already satisfied: joblib>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from scikit-learn->mapclassify) (1.2.0)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.8.1->pandas>=1.0->mapclassify) (1.16.0)
In [87]:
import geopandas as gpd
import numpy as np
import shapely.geometry
import folium


target_SGG = Kyggi_SGG[(Kyggi_SGG['SGG_NM']=='성남시 분당구') | (Kyggi_SGG['SGG_NM']=='성남시 중원구') | (Kyggi_SGG['SGG_NM']=='성남시 수정구')]
buffer_Size_in_km = 8

# select a point randomly from total bounds of geometry
buffer_df = gpd.GeoDataFrame(
    geometry=[
        shapely.geometry.Point(
            np.random.uniform(*target_SGG.total_bounds[[0, 2]], size=1)[0],
            np.random.uniform(*target_SGG.total_bounds[[1, 3]], size=1)[0],
        )
    ],
    crs=target_SGG.crs,
)

buffer_df = gpd.GeoDataFrame(
    geometry=buffer_df.to_crs(buffer_df.estimate_utm_crs())
    .buffer(8 * 10**3)
    .to_crs(buffer_df.crs)
)


# need overlay not sjoin
intersection_MeshBlock = gpd.overlay(buffer_df, target_SGG, how="intersection")

m = target_SGG.explore(name="base", style_kwds={"fill":False})
m = buffer_df.explore(m=m, name="buffer", style_kwds={"fill":False})
m = intersection_MeshBlock.explore(m=m, name="intersection", style_kwds={"fillColor":"orange"})
folium.LayerControl().add_to(m)

m
Out[87]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [89]:
m = folium.Map((latitude, longitude), max_zoom=20, zoom_start=16)
folium.Marker((latitude, longitude), popup='가천대학교 AI관').add_to(m)
m
Out[89]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [96]:
#target_point = gpd.GeoDataFrame(geometry=[longitude, latitude])

buffer_Size_in_km = 8
buffer_distance_m = buffer_Size_in_km * 1000

def circle(lon, lat, buffer_m):
    radius = buffer_distance_m
    local_azimuthal_projection = "+proj=aeqd +R=6378000 +units=m +lat_0={} +lon_0={}".format(lat, lon)
    wgs84_to_aeqd = partial(pyproj.transform, pyproj.Proj("+proj=longlat +datum=WGS84 +no_defs"), pyproj.Proj(local_azimuthal_projection))
    aeqd_to_wgs84 = partial(pyproj.transform, pyproj.Proj(local_azimuthal_projection), pyproj.Proj("+proj=longlat +datum=WGS84 +no_defs"))

    center = Point(float(lon), float(lat))
    point_transformed = transform(wgs84_to_aeqd, center)
    buffer = point_transformed.buffer(radius/1.4, resolution=64)
    circle_poly = transform(aeqd_to_wgs84, buffer)
    elevation=LineString(list(circle_poly.exterior.coords))
    return elevation

elevation = circle(longitude, latitude, buffer_distance_m)
test = pd.DataFrame({'geometry':[elevation]})
test =gpd.GeoDataFrame(test , geometry='geometry', crs="EPSG:4326")

m = folium.Map([latitude, longitude], control_scale=True)
tooltip=folium.GeoJson(test , name='circle').add_to(m)
folium.LayerControl().add_to(m)
m
/usr/local/lib/python3.10/dist-packages/shapely/ops.py:274: FutureWarning: This function is deprecated. See: https://pyproj4.github.io/pyproj/stable/gotchas.html#upgrading-to-pyproj-2-from-pyproj-1
  return type(geom)(zip(*func(*zip(*geom.coords))))
/usr/local/lib/python3.10/dist-packages/shapely/ops.py:276: FutureWarning: This function is deprecated. See: https://pyproj4.github.io/pyproj/stable/gotchas.html#upgrading-to-pyproj-2-from-pyproj-1
  shell = type(geom.exterior)(zip(*func(*zip(*geom.exterior.coords))))
Out[96]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [81]:
# define the target point
target_point = shapely.geometry.Point(longitude, latitude)

# calculate 8km buffer around the target point
buffer_Size_in_km = 8
buffer_distance_m = buffer_Size_in_km / 1000
buffer_polygon = target_point.buffer(buffer_distance_m)

# convert buffer polygon to a GeoDataFrame
buffer_df = gpd.GeoDataFrame(geometry=[buffer_polygon], crs=target_SGG.crs)

# find intersection between buffer and target geometry
intersection = gpd.overlay(buffer_df, target_SGG, how="intersection")

# create folium map centered on target point
map_center = [latitude, longitude]
m = folium.Map(location=map_center, zoom_start=12, control_scale = True)

# add target geometry to map
folium.GeoJson(target_SGG).add_to(m)

# add buffer polygon to map
folium.GeoJson(buffer_polygon).add_to(m)

# add intersection to map
folium.GeoJson(intersection).add_to(m)

# add layer control to map
folium.LayerControl().add_to(m)

# show map
m
Out[81]:
Make this Notebook Trusted to load map: File -> Trust Notebook

END CODE¶